
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;

using Swaf.BizObj;


namespace Swaf.BizObj.basic
{
	/// <summary>
	/// Summary description for FullBizObjCursor.
	/// </summary>
	[Serializable]
	public class FullBizObjCursor : IBizObjCursor
	{
		protected IBizObjList m_source;
		protected int m_cursorPos;

		public FullBizObjCursor(IBizObjList objs)
		{
			m_source = objs;
			m_cursorPos = 0;
		}
		#region IBizObjCursor Members

		public IBizObj currentElement
		{
			get
			{
				if (m_cursorPos >= 0 && m_cursorPos < m_source.numElements)
					return m_source[m_cursorPos];
				return null;
			}
		}

		public void deleteCurrent()
		{
			m_source.removeAt(m_cursorPos);
		}

		public bool eof
		{
			get
			{
				if (numElements == 0)
					return true;
				if (m_cursorPos == numElements)
					return true;

				return false;
			}
		}

		public bool bof
		{
			get
			{
				if (numElements == 0)
					return true;
				if (m_cursorPos == -1)
					return true;
				return false;
			}
		}

		public void moveTo(int pos)
		{
			if (pos < m_source.numElements)
			{
				m_cursorPos = pos;
			}
			else
			{
				throw new System.IndexOutOfRangeException(String.Format("Index {0} is out of range.", pos));
			}
		}

		public void movePrevious()
		{
			if (m_cursorPos > 0)
				--m_cursorPos;
		}

		public int numElements
		{
			get
			{
				return m_source.numElements;
			}
		}

		public void moveLast()
		{
			m_cursorPos = m_source.numElements - 1;
		}


		public void refresh()
		{
			//Nothing to do in this because a fullbizobjcursor points to live data.
		}

		public void moveFirst()
		{
			m_cursorPos = 0;
		}


		public void moveNext()
		{
			if (m_cursorPos < numElements)
				++m_cursorPos;
		}

		public int currentPosition
		{
			get
			{
				return m_cursorPos;
			}
		}

		public object Clone()
		{
			return null;
		}

		public IBizObjList Source
		{
			get { return m_source; }
		}

		#endregion
	}
}
